//
//  AVDBottomTabTool.m
//  AVD_Demo
//
//  Created by 3tee on 2022/11/16.
//

#import "AVDBottomTabTool.h"
#import "UIButton+AVDRibbon.h"
#import "FTPopOverMenu.h"
#import "AppDelegate.h"

@implementation AVDBottomTabTool

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
     
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    NSArray *titles = @[@"音频",@"视频",@"共享",@"白板",@"更多",];
    NSArray *norImgs = @[@"ic_mic_off",@"ic_cam_off",@"ic_screen_off",@"ic_board_off",@"ic_more"];
    NSArray *selImgs = @[@"ic_mic_on",@"ic_cam_on",@"ic_screen_on",@"ic_board_on",@"ic_more"];
    NSArray *selectors = @[@"micBtnClick:",@"camBtnClick:",@"screenBtnClick:",@"boardBtnClick:",@"moreBtnClick:"];
    CGFloat padding = 15;
    CGFloat magin = 10;
    CGFloat w = (self.frame.size.width - padding * 2 - magin * (titles.count -1))/ titles.count;
    CGFloat h = self.frame.size.height;
    for (int i = 0; i < titles.count; i++) {
       UIButton *btn = [self buttonWithTitle:titles[i] imgNorName:norImgs[i] selName:selImgs[i] selectorName:selectors[i]];
       btn.frame = CGRectMake(padding + i * (w + magin), 0, w, h);
        [self  addSubview:btn];
    }
     
}

- (UIButton *)buttonWithTitle:(NSString *)title imgNorName:(NSString *)name selName:(NSString *)selName selectorName:(NSString *)selectorName{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //[btn setBackgroundColor:[UIColor cyanColor]];
    [btn setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selName] forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn setImageLayout:UIButtonLayoutImageTop space:5];
    [btn addTarget:self action:NSSelectorFromString(selectorName) forControlEvents:UIControlEventTouchUpInside];
    btn.isSizeToFit = true;
    if([title isEqual:@"音频"]){
        [btn setSelected:[[NSUserDefaults standardUserDefaults] boolForKey:micKey]];
    }
    if([title isEqual:@"视频"]){
        [btn setSelected: [[NSUserDefaults standardUserDefaults] boolForKey:camKey]];
    }
    
    return  btn;
}

- (void)micBtnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    NSLog(@"mic clicked");
    if(self.micBlock){
        self.micBlock(btn.isSelected);
    }
}

- (void)camBtnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    NSLog(@"cam clicked");
    if(self.camBlock){
        self.camBlock(btn.isSelected);
    }
}

- (void)screenBtnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    NSLog(@"switch clicked");
    if(self.screenBlock){
        self.screenBlock(btn.isSelected);
    }
    
}

- (void)boardBtnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    NSLog(@"board clicked");
    if(self.boardBlock){
        self.boardBlock(btn.isSelected);
    }
}

- (void)moreBtnClick:(UIButton *)btn{
    NSString *icomImageURLString = @"https://avatars1.githubusercontent.com/u/4414522?v=3&s=40";
    NSURL *icomImageURL = [NSURL URLWithString:icomImageURLString];
    [FTPopOverMenu showForSender:btn
                   withMenuArray:@[@"用户列表", @"切换", @"聊天",]
                      imageArray:@[[UIImage imageNamed:@"ic_users"],[UIImage imageNamed:@"ic_cam_switch"],[UIImage imageNamed:@"ic_msg"]]
                       doneBlock:^(NSInteger selectedIndex) {
        switch (selectedIndex) {
            case 0:{
                if(self.usersBlock){
                    self.usersBlock();
                }
            }
                break;
            case 1:{
                if(self.swithBlock){
                    self.swithBlock(btn.isSelected);
                }
            }
                break;
            case 2:{
                if(self.chatBlock){
                    self.chatBlock();
                }
            }
                break;
                
            default:
                break;
        }
     
                           
                       } dismissBlock:^{
                           
                           NSLog(@"user canceled. do nothing.");
                           

                           
                       }];

    
}


@end
